﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
    /// <summary>
    /// This object represents the methods of a Gallery.
    /// </summary>
    internal class GalleryDAO
    {

        #region general
        private static Gallery LoadObj(IDataReader datareader)
        {
            Gallery obj = new Gallery();
            obj.GalleryID = MpDataHelp.GetInt(datareader, "GalleryID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Images = MpDataHelp.GetString(datareader, "Images");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.CreateDate = MpDataHelp.GetDateTime(datareader, "CreateDate");

            return obj;
        }
        #endregion

        public static int Gallery_Add(Gallery obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Gallery_Add");
            db.AddOutParameter(dbCommand, "GalleryID", DbType.Int32, obj.GalleryID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Images", DbType.String, obj.Images);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@GalleryID"));
            return -1;
        }
        public static int Gallery_Update(Gallery obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Gallery_Update");
            db.AddInParameter(dbCommand, "GalleryID", DbType.Int32, obj.GalleryID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Images", DbType.String, obj.Images);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Gallery_Delete(Int32 GalleryID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Gallery_Delete");
            db.AddInParameter(dbCommand, "GalleryID", DbType.Int32, GalleryID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Gallery> Gallery_List(int GalleryID, int CategoryID, int RowNumber, int IsActive)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Gallery_List");
            db.AddInParameter(dbCommand, "GalleryID", DbType.Int32, GalleryID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "RowNumber", DbType.Int32, RowNumber);
            db.AddInParameter(dbCommand, "IsActive", DbType.Int32, IsActive);
            List<Gallery> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Gallery>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



